package com.wuming.house.service.impl;

import com.wuming.house.dao.InformationMapper;
import com.wuming.house.model.Information;
import com.wuming.house.service.InformationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuhuai
 * @createTime 2019/8/27 14:54
 * @Description:
 */
@Service
public class InformationServiceImpl extends BaseServiceImpl implements InformationService {

    @Autowired
    private InformationMapper informationMapper;

    @Override
    public List<Information> getInfosByPage(String title, String source, int pageIndex, int pageSize) {
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(title)){
            params.put("title",title);
        }
        if (StringUtils.isNotBlank(source)){
            params.put("source",source);
        }
        try {
            return (List<Information>) this.getPageList(InformationMapper.class,"selectInfos",params,pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getTotal(String title, String source) {
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(title)){
            params.put("title",title);
        }
        if (StringUtils.isNotBlank(source)){
            params.put("source",source);
        }
        try {
            return this.getTotal(InformationMapper.class,"selectTotal",params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Information getInformationById(Integer id) {
        try {
            return informationMapper.selectInformation(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void saveInformation(Information information) {
        try {
            informationMapper.insert(information);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void modifyInformation(Information information) {
        try {
            informationMapper.update(information);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            informationMapper.delete(id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
